package com.hippomeetsskunk.savanna.types.world;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;

import com.hippomeetsskunk.savanna.model.ModelProvider;
import com.hippomeetsskunk.savanna.types.person.Person;
import com.hippomeetsskunk.savanna.types.resource.LocatedObject;

public class Region implements Location {

	final private String name;
	private Clima clima;
	private Weather weather;
	
	private Collection<Person> persons = new HashSet<Person>();
	private Collection<LocatedObject> locations = new HashSet<LocatedObject>();
	
	public Region(String name, Clima clima, Weather weather){
		this.name = name;
		this.clima = clima;
		this.weather = weather;
	}
	
	public String getName(){
		return name;
	}
	
	public void startDay(Time time){
		ModelProvider.getInstance().getWeatherModel().nextDay(weather, clima, time);
		
		for(LocatedObject l : locations){
			l.startDay(time, this);
		}
		
		Iterator<Person> it = persons.iterator();
		while(it.hasNext()){
			Person p = it.next();
			boolean b = p.startDay(time, this);
			if (!b){
				System.out.println(p + " has died.");
				it.remove();
			}
		}
	}
	
	public Weather getWeather(){
		return weather;
	}

	public void add(Person person){
		this.persons.add(person);
	}
	
	public void add(LocatedObject object){
		this.locations.add(object);
	}
	
	public void remove(Person person){
		this.persons.remove(person);
	}
	
	public void remove(LocatedObject object){
		this.locations.remove(object);
	}
	
	public Collection<Person> getPersons(){
		return Collections.unmodifiableCollection(persons);
	}
	
	@Override
	public String toString() {
		return "Region(" + name + ")";
	}
	
}
